﻿using PublicEntity;
using SocketServer.Model;

namespace SocketServer
{
    /// <summary>
    /// Fleck.IWebSocketConnection扩展方法
    /// </summary>
    public static class IWebSocketConnectionExtend
    {
        /// <summary>
        /// 返回Json格式文件
        /// </summary>
        /// <param name="socket">socket</param>
        /// <param name="Data">数据</param>
        public static void Json(this Fleck.IWebSocketConnection socket, object Data)
        {
            socket.Send(Newtonsoft.Json.JsonConvert.SerializeObject(Data));
        }

        /// <summary>
        /// 返回失败Json
        /// </summary>
        /// <param name="socket">socket</param>
        /// <param name="Message">返回的错误消息</param>
        /// <param name="Data">返回的错误数据源</param>
        /// <param name="Status">状态码</param>
        public static void RError(this Fleck.IWebSocketConnection socket, string Message, object Data = null, int Status = 400)
        {
            socket.Json(new ResponseResult(false)
            {
                Message = Message,
                Data = Data,
                Status = Status
            });
        }

        /// <summary>
        /// 返回成功Json
        /// </summary>
        /// <param name="socket">socket</param>
        /// <param name="Message">返回的错正确消息</param>
        /// <param name="Data">返回的正确数据源</param>
        /// <param name="Status">状态码</param>
        public static void RSuccess(this Fleck.IWebSocketConnection socket, string Message, object Data = null, int Status = 200)
        {
            socket.Json(new ResponseResult(true)
            {
                Message = Message,
                Data = Data,
                Status = Status
            });
        }

        /// <summary>
        /// 返回失败Json
        /// </summary>
        /// <param name="Request">socket</param>
        /// <param name="Message">返回的错误消息</param>
        /// <param name="Data">返回的错误数据源</param>
        /// <param name="Status">状态码</param>
        public static void RError(this SocketRequest Request, string Message, object Data = null, int Status = 400)
        {
            if (Request == null || Request.Socket == null)
            {
                return;
            }
            Request.Socket.Json(new ResponseResult(false)
            {
                Message = Message,
                Data = Data,
                Status = Status,
                ActionName = Request.ActionName,
                ClassName = Request.ClassName
            });
        }
    }
}
